package com.thoughtworks.android.androsphere.framework;

import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.c2dm.C2DMBaseReceiver;
import com.thoughtworks.android.androsphere.service.RegistrationServiceClient;
import com.thoughtworks.android.androsphere.service.ServiceClient;

public class C2DMReceiver extends C2DMBaseReceiver {
    private static final String TAG = C2DMReceiver.class.getName();

    public C2DMReceiver() {
        // TODO: Need to see how to pass sender Id here
        super("");
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        String mesg = intent.getExtras().getString("mesg");
        String source = intent.getExtras().getString("source");

        Log.d(TAG, "Got message from C2DM : [" + mesg + "]");

        new CloudMessageNotifier(context).postNotification("NewMessage", source, mesg);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.d(TAG, "Got error message from C2DM : [" + errorId + "]");
        //TODO: need to find out how this can be propagated to the activity
    }

    @Override
    public void onRegistered(Context context, String registrationId) throws IOException {
        ServiceClient registrationServiceClient = new RegistrationServiceClient(context, registrationId);
        registrationServiceClient.execute();
        Log.d(TAG, "Registered with id : " + registrationId);
    }

    @Override
    public void onUnregistered(Context context) {
        Log.d(TAG, "Got de-registered ");
    }
}
